//
//  ZSNetworkTools.swift
//  3.Swift中网络的封装
//
//  Created by heima on 16/4/8.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import AFNetworking

//swift 中的枚举
enum ZSHttpMethods: String {
    case POST = "POST"
    case GET = "GET"
}


//结构体中声明的成员变量需要用stati进行修饰
struct API {
    //线上的baseURL
   static let onlineHost = "https://api.weibo.com/"
    //测试的baseURL
   static let testHost = "https://dev.api.weibo.com"
    //获取授权的接口
   static let accessToken = "oauth2/access_token"
    //请求用户数据的接口
   static let user =  "2/users/show.json"
    //请求首页数据的接口
static let homeData = "2/statuses/home_timeline.json"

}


class ZSNetworkTools: AFHTTPSessionManager {
    //声明单例对象
    static let sharedTools: ZSNetworkTools = {
        
        let tools = ZSNetworkTools(baseURL: NSURL(string: API.onlineHost))
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    //封装所有的网络请求方法 所有的网络请求都是通过这个方法和 AFN进行联系

    func request(method: ZSHttpMethods,urlString: String, parameters: AnyObject?, finished: (result: AnyObject?, error: NSError?) -> ()) {
        
        //swift中就不能够使用利用协议 欺骗Xcode
        //为了达到封装的目的 可以将相同的闭包 抽取出来,当做参数传递给AFN框架
        let success =  { (task: NSURLSessionDataTask, result: AnyObject?) -> Void in
            //执行成功的回调
            finished(result: result, error: nil)
        }
        
        //定义失败的回调
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> Void in
            //执行失败的回调
            finished(result: nil, error: error)
        }
        
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }

    }

}

//为什么要对网络工具了进行进一步的封装??面向接口,更好的管理接口
extension ZSNetworkTools{

    //请求-->请求传了一个回调的参数,请求结束进行回调,把请求到的数据进行传递
    func loadAccessToken(code:String,finished:(result:AnyObject?,error:NSError?)->()){
        /**拼接参数*/
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type": "authorization_code","code":code,"redirect_uri":redirect_uri]

        request(.POST, urlString: API.accessToken, parameters: parameters, finished: finished)

    }
    
    
    
    
    
    
    //这个user数据是登录用户的数据,就是自己的数据
    func loadUserData(dict:[String: AnyObject],result:AnyObject,finished:(result:AnyObject?,error:NSError?)->()){
    

        let token = dict["access_token"]!
        
        let uid = dict["uid"]!
        

        
        let parameters = ["access_token": token, "uid": uid]

        //核心代码进行网络请求
        request(.GET, urlString: API.user, parameters: parameters, finished: finished)
    
    }

    /*为什么要进行进一步的封装??为了更方便的管理api等,经过上面的步骤,把获取授权和获取用户数据的网络请求的代码放在了网络工具类中*/

    
    
    //添加一个方法获取home页的数据这个方法调用完毕会返回一个请求到的数据
    func loadHomeVCData(access_token:String,finished:(result: AnyObject?, error: NSError?) -> ()){

        request(.GET, urlString: API.homeData, parameters: ["access_token":access_token], finished: finished)
    }
}


